package cn.fighter3.util;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import cn.fighter3.entity.LoginUser;
import cn.fighter3.entity.User;

/**
 * @author link
 * @description:
 * @date 2023-09-01 16:53
 */
public class SecurityUtils {

    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static LoginUser getUser(Authentication authentication) {
        Object principal = authentication.getPrincipal();
        return principal instanceof LoginUser ? (LoginUser)principal : null;
    }

    public static User getUser() {
        Authentication authentication = getAuthentication();
        LoginUser loginUser = getUser(authentication);
        if (ObjectUtils.isNotEmpty(loginUser)) {
            return loginUser.getUser();
        }
        return null;
    }

}
